//
//  CustomNavigationView.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/14.
//

import UIKit

class CustomNavigationView: UIView {
    
    var leftItems:Array<CustomBarButtonItem>{
        set{
            self.updateLeftItems(newValue)
        }
        get{
            return self.leftButtons
        }
    }
    private var leftButtons:Array<CustomBarButtonItem> = []
    var rightItems:Array<CustomBarButtonItem>{
        set{
            self.updateRightItems(newValue)
        }
        get{
            return self.rightButtons
        }
    }
    private var rightButtons:Array<CustomBarButtonItem> = []
    
    var title:String{
        set{
            titleView.text = newValue
        }
        get{
            return titleView.text ?? ""
        }
    }
    private let leftView = UIView()
    private let titleView = UILabel()
    private let rightView = UIView()
    
    init() {
        super.init(frame: .zero)
        backgroundColor = .white
        addSubview(leftView)
        addSubview(titleView)
        addSubview(rightView)
        titleView.textAlignment = .center
        leftView.snp.makeConstraints {
            $0.bottom.equalTo(0)
            $0.top.equalTo(UIApplication.shared.statusBarFrame.height)
            $0.leading.equalToSuperview()
        }
        leftView.backgroundColor = .red
        titleView.backgroundColor = .blue
        rightView.backgroundColor = .cyan
        titleView.snp.makeConstraints {
            $0.leading.equalTo(leftView.snp.trailing)
            $0.width.greaterThanOrEqualTo(leftView)
            $0.bottom.equalTo(0)
            $0.top.equalTo(UIApplication.shared.statusBarFrame.height)
        }
        
        rightView.snp.makeConstraints {
            $0.leading.equalTo(titleView.snp.trailing)
            $0.bottom.equalTo(0)
            $0.top.equalTo(UIApplication.shared.statusBarFrame.height)
            $0.width.equalTo(snp.height)
            $0.right.trailing.equalToSuperview()
        }
        
        let backItem = CustomBarButtonItem.init(image: UIImage(named: "back"), target: self, action: #selector(backItemClick))
        leftItems = [backItem]
       
    }

    @objc func backItemClick(){
        if self.fatherViewController()?.navigationController == nil {
            self.fatherViewController()?.dismiss(animated: true, completion: {
                
            })
        }else{
            self.fatherViewController()?.navigationController?.popViewController(animated: true)
        }
    }
    private func updateLeftItems(_ items:Array<CustomBarButtonItem>){
        leftButtons = leftButtons.filter({ (item) -> Bool in
            item.removeFromSuperview()
            return false
        })
        leftButtons = items
        var temp:UIView?
        for i in 0..<items.count {
            let item = items[i]
            leftView.addSubview(item)
            item.snp.makeConstraints({
                if temp == nil{
                    $0.left.equalTo(0)
                }else{
                    $0.left.equalTo((temp?.snp.right)!)
                }
                if i == items.count-1{
                    $0.right.equalTo(0)
                }
                $0.height.centerY.equalToSuperview()
            })
            temp = item
        }
        
    }
    private func updateRightItems(_ items:Array<CustomBarButtonItem>){
        rightButtons = rightButtons.filter({ (item) -> Bool in
            item.removeFromSuperview()
            return false
        })
        rightButtons = items
        var temp:UIView?
        for i in 0..<items.count {
            let item = items[i]
            leftView.addSubview(item)
            item.snp.makeConstraints({
                if temp == nil{
                    $0.left.equalTo(0)
                }else{
                    $0.left.equalTo((temp?.snp.right)!)
                }
                if i == items.count-1{
                    $0.right.equalTo(0)
                }
                $0.height.centerY.equalToSuperview()
            })
            temp = item
        }
    }
    required init?(coder: NSCoder) {
        fatalError()
    }
}

class CustomBarButtonItem : UIView {
    var font:UIFont{
        set{
            btn.titleLabel?.font = newValue
        }
        get{
           return btn.titleLabel!.font
        }
    }
    private let btn = UIButton(type: .custom)
    init(title:String,target:Any?,action:Selector) {
        super.init(frame: .zero)
        addSubview(btn)
        btn.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        btn.setTitle(title, for: .normal)
        btn.setTitleColor(B000_COLOR, for: .normal)
        btn.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
        btn.addTarget(target, action: action, for: .touchUpInside)
        font = Font_Size_14
    }
    init(image:UIImage?,target:Any?,action:Selector) {
        super.init(frame: .zero)
        btn.contentEdgeInsets = UIEdgeInsets.init(top: 0, left: 15, bottom: 0, right: 15)
        addSubview(btn)
        btn.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        btn.setImage(image, for: .normal)
        btn.addTarget(target, action: action, for: .touchUpInside)
    }

    override var tintColor: UIColor!{
        set{
            btn.tintColor = newValue
        }
        get{
            return btn.tintColor
        }
    }
    
    func setTitle(_ title:String,state:UIControl.State) {
        btn.setTitle(title, for: state)
    }
    func setTitleColor(_ color:UIColor,state:UIControl.State) {
        btn.setTitleColor(color, for: state)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
